package com.bigknow.appstore.web.console.service.impl;

import com.bigknow.appstore.common.inf.server.common.AppInfoHandler;
import com.bigknow.appstore.web.base.entity.FusionConfig;
import com.bigknow.appstore.web.base.service.impl.FusionService;
import com.bigknow.appstore.web.console.app.Application;
import com.bigknow.net.minidev.json.JSONObject;
import com.bigknow.net.minidev.json.JSONValue;
import org.mongodb.morphia.Datastore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by chaos on 2016/10/20.
 */
@Service
public class AppInfoService implements AppInfoHandler {

    @Autowired
    private Datastore ds;

    @Autowired
    private FusionService fusionService;

    @PostConstruct
    public void init() {
        fusionService.registerRPCInterface(AppInfoHandler.class, this);
    }

    @Override
    public String getAppState(String id) {
        Application app = ds.createQuery(Application.class)
                .retrievedFields(true, "state")
                .field("id").equal(id).get();
        if (app != null) {
/*            JSONObject jsonObject = new JSONObject();
            jsonObject.put("appState", app.getState().toString());
            FusionConfig fusionConfig = ds.find(FusionConfig.class).get();
            jsonObject.put("driverClass", fusionConfig.getDriverClass()==null?"":fusionConfig.getDriverClass());
            jsonObject.put("jdbcUrl", fusionConfig.getJdbcUrl()==null?"":fusionConfig.getJdbcUrl());
            jsonObject.put("userName", fusionConfig.getUserName()==null?"":fusionConfig.getUserName());
            jsonObject.put("password", fusionConfig.getPassword()==null?"":fusionConfig.getPassword());*/
            StringBuilder sb = new StringBuilder();
            FusionConfig fusionConfig = ds.find(FusionConfig.class).get();
            sb.append("{")
                    .append("\"appState\":").append(String.format("\"%s\"", app.getState().toString()))
//                    .append(",")
//                    .append("\"driverClass\":").append(String.format("\"%s\"", fusionConfig.getDriverClass() == null ? "" : fusionConfig.getDriverClass()))
//                    .append(",")
//                    .append("\"jdbcUrl\":").append(String.format("\"%s\"", fusionConfig.getJdbcUrl() == null ? "" : fusionConfig.getJdbcUrl()))
//                    .append(",")
//                    .append("\"userName\":").append(String.format("\"%s\"", fusionConfig.getUserName() == null ? "" : fusionConfig.getUserName()))
//                    .append(",")
//                    .append("\"password\":").append(String.format("\"%s\"", fusionConfig.getPassword() == null ? "" : fusionConfig.getPassword()))
                    .append("}");
            return sb.toString();
        } else {
            return "";
        }
    }

    public static void main(String[] args) throws IOException {
        URL u = new URL("http://127.0.0.1:9290/dv/rest/appState/1476093668388");
        URLConnection urlConnection = u.openConnection();
        urlConnection.connect();
        byte[] by = new byte[urlConnection.getInputStream().available()];
        urlConnection.getInputStream().read(by);
        String value = new String(by);
        System.out.println(value);
        JSONObject jsonObject = (JSONObject) JSONValue.parse(value);
        System.out.println(jsonObject.toJSONString());
    }

}
